package com.fjzx.androidframe.base.aop;

import com.fjzx.androidframe.base.aop.annotation.Permission;
import com.fjzx.androidframe.base.util.XPermission;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

import java.util.List;

/**
 * 权限申请Aop切面
 * 用于Android6.0+ 的动态权限处理
 *
 * @author xiongyongcong
 */
@Aspect
public class PermissionAspect {
    @Around("execution(@com.fjzx.androidframe.base.aop.annotation.Permission * *(..))")
    public void aroundJoinPoint(ProceedingJoinPoint joinPoint) throws Throwable {
        try {
            Permission annotation = AspectJUtil.getMethodAnnotation(joinPoint, Permission.class);
            String[] permissions = annotation.value();
            XPermission.permission(permissions).rationale(shouldRequest -> shouldRequest.again(true)).callback(new XPermission.FullCallback() {
                @Override
                public void onGranted(List<String> permissionsGranted) {
                    try {
                        joinPoint.proceed();
                    } catch (Throwable t) {
                        t.printStackTrace();
                    }
                }

                @Override
                public void onDenied(List<String> permissionsDeniedForever, List<String> permissionsDenied) {
                }
            }).request();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
